package com.nari.ysphd.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.nari.ysphd.R;
import com.nari.ysphd.fragment.ConcludedMeetingsFragment;
import com.nari.ysphd.fragment.ForthcomingMeetingsFragment;
import com.nari.ysphd.fragment.OngoingMeetingsFragment;

/**
 * Created by jackeysun on 2019/4/2.
 */

public class MeetingSearchActivity extends BaseActivity {

    private static final String TAG = "MeetingSearchActivity";
    private EditText search_content;
    private TextView search_cancel;
    private static final String MODEL_FLAG = "model_flag";

    public static void startActivity(Activity activity, int flag) {
        Intent intent = new Intent(activity, MeetingSearchActivity.class);
        intent.putExtra(MODEL_FLAG, flag);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meeting_search);

        search_content = (EditText) findViewById(R.id.search_content);
        search_cancel = (TextView) findViewById(R.id.search_cancel);


        search_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        search_content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                searchMeetings(s.toString());
            }
        });
    }

    private void searchMeetings(String meetingOfWord) {
        int flag = getIntent().getIntExtra(MODEL_FLAG, 0);
        Log.d(TAG, "searchMeetings: " + flag);
        Fragment fragment =OngoingMeetingsFragment.newInstance(meetingOfWord);
        if (flag == 0){
            fragment = OngoingMeetingsFragment.newInstance(meetingOfWord);
        }else if (flag == 1){
            fragment = ForthcomingMeetingsFragment.newInstance(meetingOfWord);
        }else if (flag == 2){
            fragment = ConcludedMeetingsFragment.newInstance(meetingOfWord);
        }

        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
    }
}
